﻿@page "/SysClientLog"
@using SelfService.ViewModel.SysClientLogVMs;
@inherits BasePage
@attribute [ActionDescription("日志管理", "SelfService.Controllers,SysClientLog")]

<WTSearchPanel OnSearch="@DoSearch">
    <ValidateForm Model="@SearchModel">
        <Row ItemsPerRow="ItemsPerRow.Three" RowType="RowType.Inline">
            <Select @bind-Value="@SearchModel.ClientId" Items="@AllSysClients" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]"></Select>
            <BootstrapInput @bind-Value="@SearchModel.Info"  />
            <BootstrapInput @bind-Value="@SearchModel.Module"  />
            <MultiSelect @bind-Value="@SearchModel.Level" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]" />
            <DateTimePicker ViewMode="DatePickerViewMode.DateTime" @bind-Value="@SearchModel.StartTime"></DateTimePicker>
            <DateTimePicker ViewMode="DatePickerViewMode.DateTime" @bind-Value="@SearchModel.EndTime"></DateTimePicker>
        </Row>
    </ValidateForm>
</WTSearchPanel>

<Table @ref="dataTable" TItem="SysClientLogView" 
        OnQueryAsync="OnSearch" 
        IsPagination="true" 
        IsStriped="true" 
        IsBordered="true" 
        ShowRefresh="false"
        ShowToolbar="true" 
        IsMultipleSelect="true" 
        ShowExtendButtons="true" 
        ShowDefaultButtons="false" 
        ShowExtendEditButton="false"
        ShowExtendDeleteButton="false"
        ShowLoading="true"
        OnDoubleClickRowCallback="OnDoubleClickRowCallback"
        style="margin-top:10px;">
    <TableColumns>

        <TableColumn @bind-Field="@context.客户端"  />
        <TableColumn @bind-Field="@context.Info" Width="500">
            <Template Context="v">
                <BootstrapTooltip Title="@v.Value">
                <div style="max-width: 500px; overflow: hidden;">
                    @v.Value
                </div>
                </BootstrapTooltip>
            </Template>
        </TableColumn>
        <TableColumn @bind-Field="@context.Module"  />
        <TableColumn @bind-Field="@context.Level"  />
        <TableColumn @bind-Field="@context.CreateTime" FormatString="yyyy-MM-dd HH:mm:ss" />
    </TableColumns>
    <TableToolbarTemplate>
        @if (IsAccessable("/api/SysClientLog/Add"))
        {
            <TableToolbarButton TItem="SysClientLogView" Color="Color.Primary" Icon="fa fa-fw fa-plus" Text="@WtmBlazor.Localizer["Sys.Create"]" OnClickCallback="OnCreateClick" />
        }
        @if (IsAccessable("/api/SysClientLog/BatchDelete"))
        {
            <TableToolbarPopconfirmButton 
                TItem="SysClientLogView" 
                Color="Color.Primary"
                Icon="fa fa-fw fa-trash-o" 
                Text="@WtmBlazor.Localizer["Sys.BatchDelete"]"
                OnConfirm="@OnBatchDeleteClick" 
                Content="@WtmBlazor.Localizer["Sys.BatchDeleteConfirm"]" 
                CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]"
                ConfirmButtonText="@WtmBlazor.Localizer["Sys.BatchDelete"]" 
                ConfirmButtonColor="Color.Danger" />
        }
        @if (IsAccessable("/api/SysClientLog/ExportExcel"))
        {
            <TableToolbarButton 
                TItem="SysClientLogView" 
                Color="Color.Primary" 
                Icon="fa fa-fw fa-download" 
                Text="@WtmBlazor.Localizer["Sys.Export"]" 
                OnClickCallback="@OnExportClick" 
                IsAsync="true" />
        }
    </TableToolbarTemplate>
    <RowButtonTemplate>
        <div style="padding-right:10px;">
            <TableCellButton 
                Size="Size.ExtraSmall" 
                Color="Color.Info" 
                Icon="fa fa-location-arrow" 
                Text="定位" 
                OnClick="() => OnLocationClick(context)" />
            @if (IsAccessable("/api/SysClientLog/{id}"))
            {
                <TableCellButton
                    Size="Size.ExtraSmall"
                    Color="Color.Info"
                    Icon="fa fa-info"
                    Text="@WtmBlazor.Localizer["Sys.Details"]"
                    OnClick="() => OnDetailsClick(context)"/>
            }
            @if (IsAccessable("/api/SysClientLog/BatchDelete"))
            {
                <PopConfirmButton
                    Icon="fa fa-trash-o"
                    Text="@WtmBlazor.Localizer["Sys.Delete"]"
                    OnConfirm="() => OnDeleteClick(context)"
                    Color="Color.Danger"
                    Size="Size.ExtraSmall"
                    Content="@WtmBlazor.Localizer["Sys.DeleteConfirm"]"
                    CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]"
                    ConfirmButtonText="@WtmBlazor.Localizer["Sys.Delete"]"
                    ConfirmButtonColor="Color.Danger"/>
            }
        </div>
    </RowButtonTemplate>
</Table>

@code{

    private SysClientLogSearcher SearchModel = new SysClientLogSearcher();

    private Table<SysClientLogView> dataTable;

    private List<SelectedItem> AllSysClients = new List<SelectedItem>();


    protected override async Task OnInitializedAsync()
    {

        AllSysClients = await WtmBlazor.Api.CallItemsApi("/api/SysClientLog/GetSysClients", placeholder: WtmBlazor.Localizer["Sys.All"]);

        await base.OnInitializedAsync();
    }

    private async Task<QueryData<SysClientLogView>> OnSearch(QueryPageOptions opts)
    {
        return await StartSearch<SysClientLogView>("/api/SysClientLog/Search", SearchModel, opts);
    }

    private void DoSearch()
    {
        dataTable.QueryAsync();
    }

    /**
     * 定位前后消息
     */
    private Task OnLocationClick(SysClientLogView item)
    {
        SearchModel = new()
        {
            ClientId = item.ClientId,
            StartTime = item.CreateTime.AddMinutes(-1),
            EndTime = item.CreateTime.AddMinutes(3)
        };
        this.DoSearch();
        return Task.CompletedTask;
    }

    private async Task OnCreateClick(IEnumerable<SysClientLogView> items)
    {
        if (await OpenDialog<Create>(WtmBlazor.Localizer["Sys.Create"]) == DialogResult.Yes)
        {
            await dataTable.QueryAsync();
        }
    }

    private async Task OnEditClick(SysClientLogView item)
    {
        if (await OpenDialog<Edit>(WtmBlazor.Localizer["Sys.Edit"], x => x.id == item.ID.ToString()) == DialogResult.Yes)
        {
            await dataTable.QueryAsync();
        }
    }

    private async Task Copy(string text)
    {
        await JSRuntime.InvokeAsync<string>("copy", text);
        await WtmBlazor.Toast.Success(WtmBlazor.Localizer["Sys.Info"], "复制成功");
    }

    private async Task OnDetailsClick(SysClientLogView item)
    {
        await OpenDialog<Details>(WtmBlazor.Localizer["Sys.Details"], x => x.id == item.ID.ToString());
    }

    private async Task OnBatchDeleteClick()
    {
        if (dataTable.SelectedRows?.Any() == true)
        {
            await PostsData(dataTable.SelectedRows.Select(x => x.ID).ToList(), $"/api/SysClientLog/batchdelete", (s) => WtmBlazor.Localizer["Sys.BatchDeleteSuccess", s]);
            await dataTable.QueryAsync();
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnDeleteClick(SysClientLogView item)
    {
        await PostsData(new List<string> { item.ID.ToString() }, $"/api/SysClientLog/batchdelete", (s) => "Sys.OprationSuccess");
        await dataTable.QueryAsync();
    }


    private async Task OnExportClick(IEnumerable<SysClientLogView> items)
    {
        if (dataTable.SelectedRows?.Any() == true)
        {
            await Download("/api/SysClientLog/ExportExcelByIds", dataTable.SelectedRows.Select(x => x.ID.ToString()).ToList());
        }
        else
        {
            await Download("/api/SysClientLog/ExportExcel", SearchModel);
        }
    }
    private async Task OnImportClick(IEnumerable<SysClientLogView> items)
    {
        if (await OpenDialog<Import>(WtmBlazor.Localizer["Sys.Import"]) == DialogResult.Yes)
        {
            await dataTable.QueryAsync();
        }
    }

    private async Task OnDoubleClickRowCallback(SysClientLogView arg)
    {
        await JSRuntime.InvokeAsync<string>("copy", arg.Info);
        await WtmBlazor.Toast.Success(WtmBlazor.Localizer["Sys.Info"], "复制成功");
    }

}
